package com.ikun.hosuserservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikun.hosuserservice.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserDetailsImpl implements UserDetailsService {

    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        //按用户名查询学员
        com.ikun.common.entity.User user = userService.getOne(new QueryWrapper<com.ikun.common.entity.User>().lambda().eq(com.ikun.common.entity.User::getPhone, phone));
        if(user == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("");
        //返回用户信息
        return new User(user.getPhone(),user.getPassword(),authorities);
    }
}
